package servlet;

import entity.Fruit;
import service.FruitService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        FruitService service = new FruitService();
        Fruit fruit = new Fruit();
        fruit.setType(request.getParameter("type"));
        fruit.setArea(request.getParameter("area"));
        fruit.setBreed(request.getParameter("breed"));
        fruit.setBrief(request.getParameter("brief"));
        fruit.setWeight(Integer.parseInt(request.getParameter("weight")));
        fruit.setPrice(Integer.parseInt(request.getParameter("price")));
        int result = service.add(fruit);
        if (result>0){
            out.println("<script>alert('添加成功！');location.href='ListServlet';</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
